package com.dbq.fast.business.main.dialog;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.dbq.fast.R;
import com.dbq.fast.util.RudenessScreenHelper;


/**
 * Created by sunkai on 2016/11/28.
 */

public class PopupWindowUtil {

    /**
     * 按钮正上方位置计算
     *
     * @param anchorView
     * @param context
     * @return
     */
    private static int[] calculatePopWindowPos(final View anchorView, Context context) {
        final int windowPos[] = new int[2];

        final int anchorLoc[] = new int[2];// 获取锚点View在屏幕上的左上角坐标位置


        anchorView.getLocationOnScreen(anchorLoc);
        final int anchorViewWidth = anchorView.getWidth();
        final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        final int screenHeight = context.getResources().getDisplayMetrics().heightPixels;

        windowPos[0] = screenWidth - anchorLoc[0] - anchorViewWidth - 60;// 60 是 .9 图左边保留区的位置
        windowPos[1] = screenHeight - anchorLoc[1] + (int) RudenessScreenHelper.pt2px(context, 10);
        return windowPos;
    }

    @NonNull
    private static PopupWindow createPopupWindow(Context context, final View view, View popupView) {
        PopupWindow popupWindow = new PopupWindow(context);
        popupWindow.setContentView(popupView);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable(context.getResources(), (Bitmap) null));

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                view.setTag(true);
            }
        });
        return popupWindow;
    }


    public static void firstConnectTips(Context context, final View view) {

        View popupView = LayoutInflater.from(context).inflate(R.layout.first_connect_tips, null);
        PopupWindow popupWindow = createPopupWindow(context, view, popupView);
        int popupViewWidth = (int) RudenessScreenHelper.pt2px(context, 480);
        /**
         * 箭头到 popupView 右边的距离
         * 等于 图片中的像素数（104）x 手机的 scaledDensity 然后，除以图片所在目录的 scaledDensity（xxhdpi 为 3，xhdpi 为2 hdpi 为 1.5）
         */
        int arrowRightPadding = (int) (104 * context.getResources().getDisplayMetrics().scaledDensity / 2);
        /**
         *  原点为 view 左下方，所以为了让箭头对准 view 中间，需要 左移动整个 popupView
         *  然后右移动 箭头到 popupView 右边的距离
         *  然后再右移 view 宽度一半
         */
        int xoff = -popupViewWidth + arrowRightPadding + view.getWidth() / 2;
        popupWindow.showAsDropDown(view, xoff, -view.getHeight() - (int) RudenessScreenHelper.pt2px(context, 170) - 20);
    }


}
